﻿/*********************************************************************
 版权所有: 版权所有(C) 2009 ,熙软研发部
 模块编号:  DishRecommended.aspx.cs
 作    者:范徐超
 编写日期:2009-09-15
 审    核:
 审核日期:
 描    述:菜式推荐页面
...........................................................................
如果模块有修改，则每次修改添加以下注释(以系统上线后为基准)：

*********************************************************************/

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Cisoft.Booking.Model;
using Cisoft.Booking.BLL;

public partial class Business_Notics_DishRecommended : WebUI.Page.SendMessagePage
{
    private DishBO dishBO = new DishBO();
    private DishMO dishMO = new DishMO();
    private SetBO setBO = new SetBO();
    private SetMO setMO = new SetMO();
    private ShortMessageBO smBO = new ShortMessageBO();
    private ShortMessageMO smMO = new ShortMessageMO();

    protected void Page_Load(object sender, EventArgs e)
    {
        

        if (!Page.IsPostBack)
        {
            BindHour();
            BindContract();
            BindDish();
            NowOrTiming(false);

            //添加双击事件
            ListBox1.Attributes.Add("ondblclick", "select1_ondblclick()");
            //添加手机验证事件
            tb_Mobile.Attributes.Add("onpropertychange", "select_onblur()");
            //添加手机号码验证失败阻止提交事件
            this.form1.Attributes.Add("onsubmit", "return select_onblur()");
        }
    }

    #region 按钮事件
    /// <summary>
    /// 新增事件
    /// </summary>
    protected void btnSave_Click(object sender, ImageClickEventArgs e)
    {
        int flag = -1;
        string strError = "";
        string mobileRight = "";
        string mobileError = "";
        string dishs = GetContent();
        //如果是立即发送
        if (rb_Now.Checked)
        {
            //获取发送框中的所有手机号码
            string[] mobiles = tb_Mobile.Text.Trim().Split(',');
            //遍历手机号码
            foreach (string mobile in mobiles)
            {
                //如果手机号码符合电信的手机格式
                if (IsMobile(mobile))
                {
                    //发送短信
                    try
                    {
                        SendMessage(mobile, dishs);
                        //获取格式正确的手机号码
                        mobileRight += mobile + ",";
                        //mobileRight = mobileRight.TrimEnd(',');
                    }
                    catch (Exception ex)
                    {
                        flag = 1;
                        strError = ex.Message;
                    }
                    if (flag == 1)
                    {
                        PageError("发送短信失败！错误信息如下：" + strError, Request.RawUrl);
                    }
                }
                //如果手机号码不符合电信的手机格式,获取格式不对的手机号码
                else
                {
                    mobileError += mobile + ",";
                    //mobileError = mobileError.EndsWith(",") ? mobileError.Remove(mobileError.Length - 1, 1) : mobileError;
                }
            }
            //写入系统短信表
            try
            {
                AddSystemMsg(mobileRight.TrimEnd(','), dishs, DateTime.Now);
            }
            catch (Exception ex)
            {
                flag = 2;
                strError = ex.Message;
            }
            if (flag == 2)
            {
                PageError("写入系统短信失败！错误信息如下：" + strError, Request.RawUrl);
            }
            if (flag == -1)
            {
                if (mobileError != "")
                {
                    PageRight("菜品推送发送成功!有号码为无效号码，请检查如下号码：" + mobileError.TrimEnd(','), "DishRecommended.aspx", "点击返回", "");
                }
                else
                {
                    PageRight("菜品推送发送成功!", "DishRecommended.aspx", "点击返回", "");
                }
            }
            
        }
        //如果是定时发送
        if (rb_Timing.Checked)
        {
            //获取定时发送的时间
            DateTime datetime = Convert.ToDateTime(tb_SendTime.Text + " " +
                ddl_SendHour.SelectedValue + ":" + ddl_SendMinite.SelectedValue + ":00 ");
            //判断定时发送的时间格式是否正确
            if (datetime <= DateTime.Now)
            {
                PageError("定时发送时间必须大于当前时间！", Request.RawUrl);
            }
            string[] mobiles = tb_Mobile.Text.Trim().Split(',');
            foreach (string mobile in mobiles)
            {
                if (IsMobile(mobile))
                {
                    //发送短信
                    try
                    {
                        SendMessage(mobile, dishs, datetime);
                        mobileRight += mobile + ",";
                        //mobileRight = mobileRight.EndsWith(",") ? mobileRight.Remove(mobileRight.Length - 1, 1) : mobileRight;
                    }
                    catch (Exception ex)
                    {
                        flag = 1;
                        strError = ex.Message;
                    }
                    if (flag == 1)
                    {
                        PageError("发送短信失败！错误信息如下：" + strError, Request.RawUrl);
                    }
                }
                else
                {
                    mobileError += mobile + ",";
                    //mobileError = mobileError.EndsWith(",") ? mobileError.Remove(mobileError.Length - 1, 1) : mobileError;
                }
            }
            //写入系统短信表
            try
            {
                AddSystemMsg(mobileRight.TrimEnd(','), dishs, datetime);
            }
            catch (Exception ex)
            {
                flag = 2;
                strError = ex.Message;
            }
            if (flag == 2)
            {
                PageError("写入系统短信失败！错误信息如下：" + strError, Request.RawUrl);
            }
            if (flag == -1)
            {
                if (mobileError != "")
                {
                    PageRight("菜品推送发送成功!有号码为无效号码，请检查如下号码：" + mobileError.TrimEnd(','), "DishRecommended.aspx", "点击返回", "");
                }
                else
                {
                    PageRight("菜品推送发送成功!", "DishRecommended.aspx", "点击返回", "");
                }
            }
        }
    }
    #endregion

    #region 绑定小时时间
    /// <summary>
    /// 绑定小时时间
    /// </summary>
    private void BindHour()
    {
        for (int i = 0; i < 24; i++)
        {
            ListItem li = new ListItem();
            li.Value = i.ToString();
            li.Text = i.ToString();

            ddl_SendHour.Items.Add(li);
        }

        for (int j = 0; j < 60; j++)
        {
            ListItem item = new ListItem();
            item.Text = j.ToString();
            item.Text = j.ToString();

            ddl_SendMinite.Items.Add(item);
        }
    }
    #endregion

    #region 选择即时或定时发送短信
    /// <summary>
    /// 选中定时发送
    /// </summary>
    protected void rb_Timing_CheckedChanged(object sender, EventArgs e)
    {
        if (rb_Timing.Checked)
        {
            //div_Timing.Visible = true;
            NowOrTiming(true);
        }
    }
    /// <summary>
    /// 选中立即发送
    /// </summary>
    protected void rb_Now_CheckedChanged(object sender, EventArgs e)
    {
        if (rb_Now.Checked)
        {
            //div_Timing.Visible = false;
            NowOrTiming(false);
        }
    }
    #endregion

    #region 点击显示添加联系人
    /// <summary>
    /// 点击显示添加联系人
    /// </summary>
    protected void btn_AddContact_Click(object sender, EventArgs e)
    {
        //全选
        if (cb_SelectAll.Checked == true)
        {
            //发送人为空
            if (tb_Mobile.Text.Trim() == "")
            {
                foreach (ListItem li in ListBox1.Items)
                {
                    tb_Mobile.Text += li.Value + ",";
                }
            }
            //发送人不为空，判断是否有重复的号码
            else
            {
                foreach (ListItem li in ListBox1.Items)
                {
                    if (tb_Mobile.Text.IndexOf(li.Value) >= 0)
                    {
                    }
                    else
                    {
                        tb_Mobile.Text += li.Value + ",";
                    }
                }
            }
        }
        //不为全选
        else
        {
            //发送人为空
            if (tb_Mobile.Text.Trim() == "")
            {
                foreach (ListItem li in ListBox1.Items)
                {
                    if (li.Selected == true)
                    {
                        tb_Mobile.Text += li.Value + ",";
                    }
                }
            }
            //发送人不为空，判断是否有重复的号码
            else
            {
                foreach (ListItem li in ListBox1.Items)
                {
                    if (li.Selected == true)
                    {
                        if (tb_Mobile.Text.IndexOf(li.Value) >= 0)
                        {
                        }
                        else
                        {
                            tb_Mobile.Text += li.Value + ",";
                        }
                    }
                }
            }
        }
    }
    #endregion

    #region 绑定OA联系人 格式为 员工姓名/手机 void BindContract()
    /// <summary>
    /// 绑定OA联系人格式为 手机/员工姓名
    /// </summary>
    protected void BindContract()
    {
        //ListBox1.Items.Add(new ListItem("测试号码/15307719728", "15307719728"));
        BindList(ListBox1);
    }
    #endregion

    #region 绑定菜式
    /// <summary>
    /// 绑定菜式
    /// </summary>
    private void BindDish()
    {
        DataSet ds = dishBO.GetAllList();

        if (ds.Tables[0].Rows.Count > 0)
        {
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                DataRow dr = ds.Tables[0].Rows[i];
                ListItem li = new ListItem();
                li.Text = dr["DishName"].ToString() + " 代码:" + dr["DishCode"].ToString();
                li.Value = dr["DishID"].ToString();

                lb_Set.Items.Add(li);
            }
        }
    }
    #endregion

    #region 写入系统短信表

    /// <summary>
    /// 写入系统短信表
    /// </summary>
    /// <param name="Mobile">接收手机号码</param>
    /// <param name="Content">短信内容</param>
    private void AddSystemMsg(string Mobile, string Content)
    {
        smMO.ShortMessageID = smBO.GetMaxId();
        smMO.ShortMessageRecive = Mobile;
        smMO.ShortMessageSend = "系统";
        smMO.ShortMessageSendTime = DateTime.Now;
        smMO.ShortMessageType = "菜品推送";
        smMO.ShortMessageContent = Content;

        smBO.Add(smMO);
    }

    /// <summary>
    /// 写入系统短信表
    /// </summary>
    /// <param name="Mobile">接收手机号码</param>
    /// <param name="Content">短信内容</param>
    private void AddSystemMsg(string Mobile, string Content, DateTime Time)
    {
        smMO.ShortMessageID = smBO.GetMaxId();
        smMO.ShortMessageRecive = Mobile;
        smMO.ShortMessageSend = "系统";
        smMO.ShortMessageSendTime = Time;
        smMO.ShortMessageType = "菜品推送";
        smMO.ShortMessageContent = Content;
        smMO.ID_FK = -1;

        smBO.Add(smMO);
    }
    #endregion

    #region 获取推送菜品内容
    private string GetContent()
    {
        string text = tb_MsgContent.Text;
        string sets = "";
        foreach (ListItem li in lb_Set.Items)
        {
            if (li.Selected == true)
            {
                sets += li.Text + ";";
            }
        }
        sets = sets.TrimEnd(';') + "。";
        return text + sets;

    }
    #endregion

    #region 发送选择时间控件状态
    private void NowOrTiming(bool flag)
    {
        if (flag)
        {
            tb_SendTime.Enabled = true;
            ddl_SendHour.Enabled = true;
            ddl_SendMinite.Enabled = true;
            CalendarExtender2.Enabled = true;
        }
        else
        {
            tb_SendTime.Enabled = false;
            ddl_SendHour.Enabled = false;
            ddl_SendMinite.Enabled = false;
            CalendarExtender2.Enabled = false;
        }
    }
    #endregion
}
